#include <arpa/inet.h>
#include <sys/socket.h>
#include <netdb.h>
#include <ifaddrs.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netinet/in.h>
#include <math.h>

#define INT32 int
#define NSLOOKUP_RESULT_FILE "./result_nslookup.txt"

#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif

typedef struct _nslookup_result{
	char status[64];
	char ans_type[64];
	char host_name[64];
	char ip_addr[64];
	char dns_svr_ip[64];
	int res_time;
}NSLOOKUP_RESULT_T;

typedef struct _nslookup{
	char state[128];
	int succ_cnt;
	int rlt_num;
	int old_rlt_num;
	NSLOOKUP_RESULT_T *result;
}NSLOOKUP_T;

static NSLOOKUP_T nslookup={"None", 0, 0, 0, NULL};

void nslookup_start()
{
	char cmd[128] = {0};

	sprintf(cmd, "nslookup www.github.com > %s", NSLOOKUP_RESULT_FILE);
	system(cmd);
}

void nslookup_diagnostic_init(int number)
{
	if(nslookup.result){
		free(nslookup.result);
		nslookup.result = NULL;
	}

	nslookup.old_rlt_num = nslookup.rlt_num;
	nslookup.rlt_num = number;
	nslookup.result = (NSLOOKUP_RESULT_T *)malloc(sizeof(NSLOOKUP_RESULT_T)*number);
}

INT32 nslookup_get_rlt_num(char *file)
{
	FILE *fp = NULL;
	char buff[512] = {0};
	char addr[64] = {0};
	int read_num = 0;
	int rlt_num = 0;

	fp = fopen(file, "r");
	if(fp == NULL)
		return rlt_num;

	while(fgets(buff, sizeof(buff), fp)){
		read_num =  sscanf(buff, "Address %*[^:]:  %s", addr);
		if(1 == read_num){
			rlt_num++;
		}
	}
	fclose(fp);
	
	return rlt_num;
}

INT32 nslookup_diagnostic_load_result(char *file)
{
	FILE *fp = NULL;
	char buff[512] = {0};
	char host[64] = {0};
	char addr[64] = {0};
	char rt_times[64] = {0};
	int read_num = 0;
	int rlt_idx = 0;
	int res_time = 0;

	fp = fopen(file, "r");
	if(fp == NULL)
		return -1;

	printf("<----- file = [%s] ----->\n", file);
	while(fgets(buff, sizeof(buff), fp)){
		//printf("--------------------- buff=[%s]\n", buff);
		read_num =  sscanf(buff, "Name:%*[ ]%s", host);
		//printf("first read_num = [%d]\n", read_num);
		if(1 == read_num){
			printf("found host = [%s]\n", host);
		}
		read_num =  sscanf(buff, "Address %*[^:]:  %s", addr);
		//printf("second read_num = [%d]\n", read_num);
		if(1 == read_num){
			strcpy(nslookup.result[rlt_idx].status, "");
			strcpy(nslookup.result[rlt_idx].ans_type, "");
			strcpy(nslookup.result[rlt_idx].host_name, host);
			strcpy(nslookup.result[rlt_idx].ip_addr, addr);
			strcpy(nslookup.result[rlt_idx].dns_svr_ip, "");
			nslookup.result[rlt_idx].res_time = 0;
			rlt_idx++;
		}
	}

	fclose(fp);
	
	return 0;
}

void nslookup_diagnostic_update_data_model()
{
#if 0
	if(nslookup.old_rlt_num != nslookup.rlt_num){
		if(nslookup.old_rlt_num > 0)
			tr69c_delObjInstance("Device.IP.Diagnostics.TraceRoute.RouteHops.", 1, nslookup.old_rlt_num);
		tr69c_addObjInstance("Device.IP.Diagnostics.TraceRoute.RouteHops.", nslookup.rlt_num);
	}
#endif
}

void nslookup_diagnostic_completed()
{
	strcpy(nslookup.state, "Completed");
	//addInformEventToList(INFORM_EVENT_DIAGNOSTICS_COMPLETE);
	//sendInform(NULL);
}

void nslookup_diagnostic_finished()
{
	int rlt_number = 0;

	rlt_number = nslookup_get_rlt_num(NSLOOKUP_RESULT_FILE);
	nslookup_diagnostic_init(rlt_number);
	nslookup_diagnostic_load_result(NSLOOKUP_RESULT_FILE);	
	//nslookup_diagnostic_update_data_model();
	nslookup_diagnostic_completed();
}

void nslookup_diagnostic_print()
{
	int idx = 0;

	printf("dbg---> %s\n", __FUNCTION__);
	printf("nslookup.state=[%s]\n", nslookup.state);
	printf("nslookup.rlt_num=[%d]\n", nslookup.rlt_num);
	for(idx = 0; idx < nslookup.rlt_num; idx++){
		printf("nslookup.[%d] status=[%s] ans_type=[%s] host_name=[%s] ip_addr=[%s] dns_svr_ip=[%s], res_time=[%d]\n", idx,
			nslookup.result[idx].status,
			nslookup.result[idx].ans_type,
			nslookup.result[idx].host_name,
			nslookup.result[idx].ip_addr,
			nslookup.result[idx].dns_svr_ip,
			nslookup.result[idx].res_time);
	}
}

int main(int argc, char *argv[])
{
	int index;

	//nslookup_start();
	//nslookup_diagnostic_load_result(NSLOOKUP_RESULT_FILE);
	nslookup_diagnostic_finished();
	nslookup_diagnostic_print();

	return 0;
}
